import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isNil from "lodash/isNil";
import { getResponse, createPagination } from 'utils/utils';
import { regionQueryLine as _regionQueryLine, regionQueryLazyTree as _regionQueryLazyTree, regionCreate as _regionCreate, regionUpdate as _regionUpdate, regionEnable as _regionEnable, regionDisable as _regionDisable, regionQueryDetail as _regionQueryDetail } from "../services/regionService";

function buildNewTreeDataSource() {
  var treeDataSource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var iterFunc = arguments.length > 1 ? arguments[1] : undefined;
  return treeDataSource.map(function (item) {
    if (item.children) {
      var newItem = iterFunc(item);
      return _objectSpread(_objectSpread({}, newItem), {}, {
        children: buildNewTreeDataSource(newItem.children, iterFunc)
      });
    } else {
      return iterFunc(item);
    }
  });
}

function transformData() {
  var dataSource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var indent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  return dataSource.map(function (item) {
    if (item.hasNextFlag === 1) {
      return _objectSpread(_objectSpread({}, item), {}, {
        indent: indent,
        children: []
      });
    } else {
      return _objectSpread(_objectSpread({}, item), {}, {
        indent: indent
      });
    }
  });
}

export default {
  namespace: 'region',
  state: {
    // 地区 打平数据存储
    lineDataSource: [],
    linePagination: {},
    // 地区 懒加载树结构
    treeDataSource: [],
    // 树结构数据
    expandKeys: [],
    // 树结构数据展开的数据
    loadingExpandKeys: [] // 加载数据中的行

  },
  effects: {
    // 查询 打平的地区
    regionQueryLine: /*#__PURE__*/_regeneratorRuntime.mark(function regionQueryLine(_ref, _ref2) {
      var payload, call, put, countryId, query, res, data;
      return _regeneratorRuntime.wrap(function regionQueryLine$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              countryId = payload.countryId, query = payload.query;
              _context.next = 5;
              return call(_regionQueryLine, countryId, query);

            case 5:
              res = _context.sent;
              data = getResponse(res);

              if (!data) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  lineDataSource: data.content,
                  linePagination: createPagination(data)
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, regionQueryLine);
    }),
    // 查询懒加载树结构
    regionQueryLazyTree: /*#__PURE__*/_regeneratorRuntime.mark(function regionQueryLazyTree(_ref3, _ref4) {
      var payload, call, put, countryId, regionId, _payload$indent, indent, queryChild, res, responseRes;

      return _regeneratorRuntime.wrap(function regionQueryLazyTree$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              countryId = payload.countryId, regionId = payload.regionId, _payload$indent = payload.indent, indent = _payload$indent === void 0 ? -1 : _payload$indent;
              queryChild = !_isNil(regionId);
              _context2.next = 6;
              return put({
                type: 'updateLoadingExpandKeys',
                payload: {
                  queryChild: queryChild,
                  regionId: regionId,
                  type: 'load'
                }
              });

            case 6:
              _context2.next = 8;
              return call(_regionQueryLazyTree, countryId, regionId);

            case 8:
              res = _context2.sent;
              responseRes = getResponse(res);

              if (!responseRes) {
                _context2.next = 13;
                break;
              }

              _context2.next = 13;
              return put({
                type: 'updateTreeDataSource',
                payload: {
                  queryChild: queryChild,
                  regionId: regionId,
                  treeData: transformData(responseRes, indent + 1)
                }
              });

            case 13:
              _context2.next = 15;
              return put({
                type: 'updateLoadingExpandKeys',
                payload: {
                  queryChild: queryChild,
                  regionId: regionId,
                  type: 'unload'
                }
              });

            case 15:
            case "end":
              return _context2.stop();
          }
        }
      }, regionQueryLazyTree);
    }),
    regionCreate: /*#__PURE__*/_regeneratorRuntime.mark(function regionCreate(_ref5, _ref6) {
      var payload, call, countryId, body, res;
      return _regeneratorRuntime.wrap(function regionCreate$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              countryId = payload.countryId, body = payload.body;
              _context3.next = 5;
              return call(_regionCreate, countryId, body);

            case 5:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, regionCreate);
    }),
    regionUpdate: /*#__PURE__*/_regeneratorRuntime.mark(function regionUpdate(_ref7, _ref8) {
      var payload, call, countryId, body, regionId, res;
      return _regeneratorRuntime.wrap(function regionUpdate$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              countryId = payload.countryId, body = payload.body, regionId = payload.regionId;
              _context4.next = 5;
              return call(_regionUpdate, countryId, regionId, body);

            case 5:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context4.stop();
          }
        }
      }, regionUpdate);
    }),
    regionEnable: /*#__PURE__*/_regeneratorRuntime.mark(function regionEnable(_ref9, _ref10) {
      var payload, call, body, regionId, res;
      return _regeneratorRuntime.wrap(function regionEnable$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              body = payload.body, regionId = payload.regionId;
              _context5.next = 5;
              return call(_regionEnable, regionId, body);

            case 5:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context5.stop();
          }
        }
      }, regionEnable);
    }),
    regionDisable: /*#__PURE__*/_regeneratorRuntime.mark(function regionDisable(_ref11, _ref12) {
      var payload, call, body, regionId, res;
      return _regeneratorRuntime.wrap(function regionDisable$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              body = payload.body, regionId = payload.regionId;
              _context6.next = 5;
              return call(_regionDisable, regionId, body);

            case 5:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, regionDisable);
    }),
    regionQueryDetail: /*#__PURE__*/_regeneratorRuntime.mark(function regionQueryDetail(_ref13, _ref14) {
      var payload, call, regionId, res;
      return _regeneratorRuntime.wrap(function regionQueryDetail$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              regionId = payload.regionId;
              _context7.next = 5;
              return call(_regionQueryDetail, regionId);

            case 5:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context7.stop();
          }
        }
      }, regionQueryDetail);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref15) {
      var payload = _ref15.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    // 由于 可能异步问题 导致 数据不一致, 所以更新放到 reducer 中
    updateTreeDataSource: function updateTreeDataSource(state, _ref16) {
      var payload = _ref16.payload;
      var queryChild = payload.queryChild,
          regionId = payload.regionId,
          treeData = payload.treeData;
      return _objectSpread(_objectSpread({}, state), {}, {
        treeDataSource: queryChild ? buildNewTreeDataSource(state.treeDataSource, function (item) {
          if (item.regionId === regionId) {
            return _objectSpread(_objectSpread({}, item), {}, {
              children: treeData
            });
          } else {
            return item;
          }
        }) : treeData,
        expandKeys: queryChild ? [].concat(_toConsumableArray(state.expandKeys), [regionId]) : []
      });
    },
    // 由于 expand 问题, expand 由自己管理
    updateLoadingExpandKeys: function updateLoadingExpandKeys(state, _ref17) {
      var payload = _ref17.payload;
      var queryChild = payload.queryChild,
          regionId = payload.regionId,
          type = payload.type;

      if (!queryChild) {
        return _objectSpread(_objectSpread({}, state), {}, {
          loadingExpandKeys: []
        });
      }

      switch (type) {
        case 'load':
          return _objectSpread(_objectSpread({}, state), {}, {
            loadingExpandKeys: [].concat(_toConsumableArray(state.loadingExpandKeys), [regionId])
          });

        case 'unload':
          return _objectSpread(_objectSpread({}, state), {}, {
            loadingExpandKeys: state.loadingExpandKeys.filter(function (k) {
              return k !== regionId;
            })
          });

        default:
          return state;
      }
    }
  }
};